﻿--[[
   @brief    control module for server_tips ui
   @author   dragonqiu
   @version  2.0
   @date     2015-12-10
--]]

server_tips_control = 
{
    -- 大尺寸和小尺寸
    max_width = 316;
    min_width = 316;
    NORMAL_TYPE = 0;
    IMAGE_TYPE = 4;
    NORMAL_TYPE_SIZE = {width = 316, height = 211};
    IMAGE_TYPE_SIZE = {width = 300, height = 430 };
    WANTPLAY_TYPE_SIZE = {width = 306, height = 152};
    opts = {};
    link ="";
    view_uilib = nil;
    view_cur_id = nil;
}

function server_tips_control:init()
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:AddLuaListener(ierd_tgp.tips_mgr.ON_SHOW_POP_TIPS,
    "server_tips_control:on_open_popup_tips");
end

function server_tips_control:poptips_show(opts)
    if poptips_model.tips_mgr ~= nil then
        poptips_model.tips_mgr:SendPopTipsOP(
        (opts.tip_id or 0), ierd_tgp.tips_mgr.KIND_TIP_DISPLAY, 0);
    end
end

function server_tips_control:close_server_tips_report(win_id, tipqosType)
    local opts = poptips_view:get_ctr_data(win_id);
    if poptips_model.tips_mgr ~= nil then
        poptips_model.tips_mgr:SendPopTipsOP(
            opts.tip_id or 0, tipqosType, 0);
    end
end

function server_tips_control:get_game_runing_state(event_id, param)
    local ptable = base_utility.json_to_table(param);
    poptips_model.game_running_state = ptable.running_state;
end;

-- TODO 间隔两秒弹出
function server_tips_control:game_end_show_tips(event_id, param)
    local tips_count = table.getn(poptips_model.waiting_tips_info);
    if tips_count > 0 then
        local i = 0;
        for key, value in pairs(poptips_model.waiting_tips_info) do
            if i >= 3 then return; end;
            local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
            tpfui:AddInterval("server_tips_control:open_save_tips",
            i * 2000, key);
            i = i + 1;
        end
    end;
end;

-- 渲染tips
function server_tips_control:show_server_tips(param)
    local info = base_utility.json_to_table(param);

    info.position = 'rbottom';
    -- 处理其他类型的tips推送
    if info.type ~= nil and info.type == 4 then
        info.width = server_tips_control.IMAGE_TYPE_SIZE.width;
        info.height = server_tips_control.IMAGE_TYPE_SIZE.height;
    else
        info.width = server_tips_control.NORMAL_TYPE_SIZE.width;
        info.height = server_tips_control.NORMAL_TYPE_SIZE.height;
    end
    info.modal = false;
    info.is_multi_win = true;
    info.ui_file = "rbpoptips.tpf"; 
    info.manuallyCloseCallback = function(win_id)
        server_tips_control:close_server_tips_report(win_id,
        ierd_tgp.tips_mgr.KIND_TIP_CLOSE);
    end;
    -- 自动关闭时的操作回调
    info.autoCloseCallback = function(win_id)
        server_tips_control:close_server_tips_report(win_id,
        ierd_tgp.tips_mgr.KIND_TIP_AUTO_CLOSE);
    end;
    
    if info.icon and info.icon ~= "" then
        if info.type == 0 then
            info.icon = "ONEIMAGE:tips\\server\\pic.png|".. info.icon .. ";Center;null;";
        elseif info.type == 4 then
            info.icon = "ONEIMAGE:tips\\server\\pic_big.png|".. info.icon .. ";Center;null;";
        end
    end
    
    if info.stay_sec ~= nil then
        if info.stay_sec > 0 then
            info.timer = tonumber(info.stay_sec) * 1000;
        end;
    end
    info.layout_callback = "server_tips_view.layout";
    info.layout_file = "poptips\\server_tips_view.lua"
    poptips_control.show_tips(info);
    self:poptips_show(info);
end;

-- 显示保存tips
function server_tips_control:open_save_tips(info_id, time_stamp)
    if poptips_model.game_running_state == 0 then
        self:show_server_tips(
        base_utility.table_to_json(
        poptips_model.waiting_tips_info[info_id]));
        if info_id >= poptips_model.winfo_num then
            poptips_model.waiting_tips_info = {};
            poptips_model.winfo_num = 0;
        end
    end
    local tpfui =
        base_utility.get_component_adapter("ITpf_ui",
            "ierd_tgp.tpf_ui.GetTPFUI();");
    tpfui:RemoveInterval("server_tips_control:open_save_tips",
    info_id);
end;

-- 显示服务器下发tips
function server_tips_control:on_open_popup_tips(event_id, param)
    -- 判断是否是想玩tips和普通tips，如果是，直接返回
    local info = base_utility.json_to_table(param);
    -- 想玩tips
    if info.type ~= nil and info.type == 5 then
        return;
    end;
    -- 普通tips
    if info.type ~= nil and info.type == 0 then
        return;
    end;

    -- 判断是否是launcher启动的wegaeme，如果是，则不显示服务器下发的tips
    local is_launcher = base_utility.is_type_of_launcher();
    if is_launcher == true then
        base_utility.log("we game is launched by game, not show server tips, server tips type = " .. info.type);
        return;
    end;

    if poptips_model.game_running_state == 0 then
        self:show_server_tips(param);
    elseif poptips_model.game_running_state > 0 then
        if poptips_model.winfo_num >= 3 then
            poptips_model.waiting_tips_info[poptips_model.winfo_num + 1] = info;
            for key, value in pairs(poptips_model.waiting_tips_info) do
                if  poptips_model.waiting_tips_info[key + 1] ~= nil then
                    poptips_model.waiting_tips_info[key] = poptips_model.waiting_tips_info[key + 1];
                end
            end
            poptips_model.waiting_tips_info[poptips_model.winfo_num + 1] = nil;
        else
            poptips_model.winfo_num = poptips_model.winfo_num + 1;
            local winfo_num = poptips_model.winfo_num;
            poptips_model.waiting_tips_info[winfo_num] = info;
        end
    end
    local info = base_utility.json_to_table(param)
    if poptips_model.tips_mgr ~= nil then
        if poptips_model.tips_mgr ~= nil then
            poptips_model.tips_mgr:SendPopTipsOP(
            (info.tip_id or 0), ierd_tgp.tips_mgr.KIND_TIP_ARRIVE, 0);
        end
    end
end;

function server_tips_control.handle_register_event(event_id, json_str)
    local param_table = base_utility.json_to_table(json_str);
    if nil == param_table then return end
    base_utility.log("handle_register_event event_name : " .. param_table.event_name);
    if param_table.event_name == "social_update_msg_event" then
        web_utility.open_link(server_tips_control.link);
        if server_tips_control.view_uilib~=nil and 
            server_tips_control.view_cur_id ~= nil then
            poptips_view:_close(server_tips_control.view_uilib,
                server_tips_control.view_cur_id);
        end
    end
    
end;

function server_tips_control:show_comment_invite_tips(info)
    info.position = 'rbottom';
    -- 提示玩家去评论游戏
    info.width = 350;
    info.height = 240;
    info.modal = false;
    info.is_multi_win = true;
    info.ui_file = "comment_invite_tips.tpf"; 
    info.hide_caption_icon = true;
    info.layout_callback = "server_tips_view.CommentInviteLayout";
    info.layout_file = "poptips\\server_tips_view.lua"
    poptips_control.show_tips(info);
    self:poptips_show(info);
end;

server_tips_control:init();
